package com.elm.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Many;
import com.elm.po.Person;
import com.elm.po.User;

@Mapper
public interface PersonMapper {
    // 插入person表（个人详情信息）
    @Insert("INSERT INTO person (id,email,first_name,last_name,gender,phone,photo)" +
            "VALUES (#{id},#{email},#{firstName},#{lastName},#{gender},#{phone},#{photo})")
    int createPerson(Person person);
    
    // 根据username查询person信息
    @Select("SELECT p.*, u.creator, u.updater, u.is_deleted, " +
            "u.activated, u.username, u.status " +
            "FROM person p JOIN users u ON u.id = p.id WHERE p.id = #{id}")
    @Results({
        // Person自身字段映射
        @Result(column = "id", property = "id"),
        @Result(column = "email", property = "email"),
        @Result(column = "first_name", property = "firstName"),
        @Result(column = "last_name", property = "lastName"),
        @Result(column = "gender", property = "gender"),
        @Result(column = "phone", property = "phone"),
        @Result(column = "photo", property = "photo"),
        // 继承自User的字段映射
        @Result(column = "creator", property = "creator"),
        @Result(column = "updater", property = "updater"),
        @Result(column = "is_deleted", property = "isDeleted"),
        @Result(column = "activated", property = "activated"),
        @Result(column = "username", property = "username"),
        @Result(column = "status", property = "status"),
        // 关联查询权限列表（复用UserAuthorityMapper的方法）
        @Result(column = "id", property = "authorities",
                many = @Many(select = "com.elm.mapper.UserAuthorityMapper.getAuthorityById"))
    })
    Person getPersonById(int id);

    
	@Update("update person set email=#{email},first_name=#{firstName},last_name=#{lastName},gender=#{gender},phone=#{phone},photo=#{photo} where id=#{id}")
	int updatePerson(Person person);
	

}